<?php

/*
 * Copyright (c) 2012 Aalto University and University of Helsinki
* MIT License
* see LICENSE.txt for more information
*/

// includes site wide settings for vocabularies and languages
require_once('config.inc');

/*
 * takes in the url and uses explode function to separate
* the different parts of the url by using '/' as a delimiter.
*/
$LANGUAGES = explode(" ", LANGUAGES);
$path = $_SERVER['PATH_INFO']; // for example "/ysa/fi"
$parts = explode('/', $path);
$number_of_parts = explode('/', $_SERVER['REQUEST_URI']);
$path_fix = (isset($parts)) ? str_repeat("../", sizeof($number_of_parts) - 3) : "";
$tmp_dir = TEMPLATE_CACHE;

// here we check if the cache pointed by config.inc exists, if not we create it.
if (!file_exists($tmp_dir)) {
  $old = umask(0);
  mkdir($tmp_dir, $mode = 0777, true);
  umask($old);
}

//if (sizeof($parts) <= 3 && $parts[sizeof($parts) - 1] != "") {
//  $url = $parts[sizeof($parts) - 1] . "/";
//  header("Location: " . $url);
//  exit();
//}

require_once('controller/Controller.php');

$controller = new Controller($path_fix, $tmp_dir);
// parsing of url-parameters and redirecting accordingly
if (sizeof($parts) <= 2) {
  // language code missing, redirect to guessed language
  $lang = $controller->guessLanguage();
  header("Location: " . $lang . "/");
} elseif (in_array('about', $parts)) {
  $lang = $parts[1];
  $controller->invokeAboutPage($lang);
} elseif (in_array('feedback', $parts)) {
  if ($parts[sizeof($parts) - 1] == "feedback" && sizeof($parts) == 3) {
    $controller->invokeFeedbackForm($parts[1]);
    exit();
  } else {
    $controller->invokeFeedbackForm($parts[2], $parts[1]);
    exit();
  }
} elseif (in_array('search', $parts)) {
  if (sizeof($parts) == 4 && $parts[3] == "search") {
    $controller->invokeVocabularySearch($parts[1], $parts[2]); exit();
  }
  elseif (sizeof($parts) == 3 && $parts[2] == "search"){
    $controller->invokeGlobalSearch($parts[1]);
  }
  
} elseif (in_array($parts[1], $LANGUAGES)) {
  $lang = $parts[1];
  if (sizeof($parts) > 3) {
    header("Location: " . $path_fix . $lang . "/");
    exit();
  }
  $controller->invokeVocabularies($lang);
} elseif (sizeof($parts) == 3) {
  // language code missing, redirect to guessed language
  $lang = $controller->guessLanguage();
  header("Location: " . $lang . "/");
  exit();
} elseif (sizeof($parts) == 4 && $parts[3] == "") {  //ysa/fi/
  $controller->invokeVocabularyHome($parts[1], $parts[2]);

// displays the vocabulary statistics page with the specified language
} elseif (sizeof($parts) == 4 && $parts[3] == "stats") {
  $controller->invokeVocabularyStats($parts[1], $parts[2]);
  // displays the vocabularys search result page with the specified language
} elseif (sizeof($parts) >= 4 && $parts[3] == "index") {
  if (sizeof($parts) == 4)
    $controller->invokeAlphabeticalSearch($parts[1], $parts[2]);
  else {
    $controller->invokeAlphabeticalSearch($parts[1], $parts[2], $parts[4]);
  }
} elseif (sizeof($parts) == 5 && $parts[3] == "page") {
  if (strstr($_SERVER['REQUEST_URI'], '?uri=')) { // testing if whole uri was requested
    // if the whole uri from a another vocab was requested parsing and passing it forward.
    $orig_uri = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '=')+1);
    $controller->invokeVocabularyConcept($parts[1], $parts[2], $orig_uri);
  } else {
    $controller->invokeVocabularyConcept($parts[1], $parts[2], $parts[4]);
  }
} else {
  $controller->invokeGenericErrorPage();
}
?>